#include "StdAfx.h"
#include "CenterProxy.h"
#include "msgCenterServer.h"
#include "ServerProxyMng.h"
#include "ServerDefines.h"

PROXY_REGIST_HELPER( CCenterProxy, enCenterServer );

CCenterProxy::CCenterProxy(void)
{
}

CCenterProxy::~CCenterProxy(void)
{
}

void CCenterProxy::RegisteMessage()
{
	RegistFunction( CS2S_CENTER_MESSAGE_TYPE, CS2S_SERVER_LOGIN_RPN, bind( &CCenterProxy::ProcessLoginRpn, this, _1, _2, _3, _4 ) );
}

xgc_lpvoid CCenterProxy::OnConnect( net::network_t h, xgc_lpvoid p )
{
	mLink = h;

	msgServerLoginRequest req;
	req.kind = enDatabaseServer;

	// send first message, regist this server information to center server.
	Send( h, req, 1024 );
	return this;
}

void CCenterProxy::OnClose( xgc_lpvoid userdata )
{
}

xgc_void CCenterProxy::ProcessLoginRpn( net::network_t h, xgc_lpvoid u, const xgc_byte* data, xgc_size size )
{
	msgServerLoginResponse rpn;
	Read( rpn, data, size );

	for( auto i = rpn.ServerList.begin(); i != rpn.ServerList.end(); ++i )
	{
		ServerProxyMng::Instance().AddServerProxy( (enServerDefine)i->kind, i->uuid, i->address, i->port );
	}
}
